//私有api集
package com.example.mybatis.ApiInterface;

import com.example.mybatis.dao.UserDao;
import com.example.mybatis.pojo.ApiResult;
import com.example.mybatis.pojo.Grade;
import com.example.mybatis.pojo.Results;
import com.example.mybatis.pojo.User;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.zip.DeflaterInputStream;

@CrossOrigin//跨域问题
@RestController
@RequestMapping("/login")
public class UserApi {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    UserDao UserDao;// = sqlSession.getMapper(UserDao.class);

    //    定义一个ApiResult方法(携带三个对应参数)
    public ApiResult result(Boolean success, String message, Object data) {
        //        定向pojo实体类
        ApiResult result = new ApiResult();
        //        将三个参数放进对应的方法中
        result.setSuccess(success);
        result.setMessage(message);
        result.setData(data);
        //        返回出去即可
        return result;
    }

    //    获取全部用户
    @GetMapping("/UserList")
    public Object UserList() {
        List<User> users = UserDao.userList();
        if (users.isEmpty()) {
            return result(false, "没有用户数据", null);
        } else {
            return result(true, "获取成功", users);
        }
    }

    //    新增用户
    @GetMapping("/useradd")
    public Object useradd(User user) {
//        数据处理
//        md5加密
        String md5pwd = DigestUtils.md5Hex(user.getUserpwd());
//        创建map
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("mail", user.getMail());
        stringObjectHashMap.put("username", user.getUsername());
        stringObjectHashMap.put("userpwd", md5pwd);
        stringObjectHashMap.put("sex", user.getSex());
        stringObjectHashMap.put("idcard", user.getIdcard());
        stringObjectHashMap.put("grade", user.getGrade());
        stringObjectHashMap.put("level", user.getLevel());
//        提交数据
        try {
            int i = UserDao.userAdd(stringObjectHashMap);
            if (i >= 1) {
                return result(true, "更新成功！", null);
            } else {
                return result(false, "更新失败！", null);
            }
        } catch (Exception e) {
            return result(false, "更新失败！" + e, null);
        }


    }

    //       通过邮箱查询用户
    @GetMapping("/showmail")
    public Object showmail(@RequestParam(value = "mail", required = true) String mail) {
        List<User> users = UserDao.mailList(mail);
        return result(true, "success", users);
    }

    //       通过邮箱查询用户是否存在
    @GetMapping("/mailList")
    public Object mailList(@RequestParam(value = "mail", required = true) String mail) {
        List<User> users = UserDao.mailList(mail);
        if (users.isEmpty()) {
            return result(true, "邮件不存在", null);
        } else {
            return result(false, "邮件已存在", null);
        }
    }


    //       通过身份证查询用户是否存在
    @GetMapping("/idcardList")
    public Object idcardList(@RequestParam(value = "idcard", required = true) String idcard) {
        List<User> users = UserDao.idcardList(idcard);
        if (users.isEmpty()) {
            return result(true, "身份证不存在", null);
        } else {
            return result(false, "身份证已存在", null);
        }
    }

    //    通过邮箱删除用户
    @GetMapping("/delectmail")
    public Object delectmail(@RequestParam(value = "mail", required = true) String mail) {
        int i = UserDao.delectmail(mail);
        if (i >= 1) {
            return result(true, "删除成功", null);
        } else {
            return result(false, "删除失败", null);
        }
    }

    //    登录检测
    @GetMapping("/login")
    public Object login(User user) {
        List<User> users = UserDao.mailList(user.getUsername());
//        判断邮箱是否存在
        if (users.isEmpty()) {
            return result(false, "用户不存在", null);
        }
//        判断密码是否正确
        if (DigestUtils.md5Hex(user.getUserpwd()).equals(users.get(0).getUserpwd())) {
//            判断权限是否符合
            if (user.getLevel() == users.get(0).getLevel()) {
                return result(true, "登录成功", users);
            } else {
                return result(false, "权限错误", null);
            }
        } else {
            return result(false, "密码错误", null);
        }
    }

    //    通过邮箱修改密码
    @GetMapping("/ModifyPwd")
    public Object ModifyPwd(@RequestParam(value = "mail", required = true) String mail, @RequestParam(value = "pwd", required = true) String pwd) {
        //        md5加密
        String md5pwd = DigestUtils.md5Hex(pwd);
        try {
            int i = UserDao.ModifyPwd(mail, md5pwd);
            if (i >= 1) {
                return result(true, "更新成功！", null);
            } else {
                return result(false, "更新失败！", null);
            }
        } catch (Exception e) {
            return result(false, "更新失败！" + e, null);
        }
    }

    @GetMapping("/showclassstudent")
    public Object showclassstudent(@RequestParam(value = "grade", required = true) String grade) {
        try {
            List<User> showclassstudent = UserDao.showclassstudent(grade);
            return result(true, "获取成功", showclassstudent);
        } catch (Exception e) {
            return result(false, "获取失败" + e, null);
        }
    }

    @GetMapping("/showTeacher")
    public Object showTeacher() {
        try {
            List<User> users = UserDao.showTeacher();
            return result(true, "获取成功", users);
        } catch (Exception e) {
            return result(false, "获取失败" + e, null);
        }
    }

    //    添加学生成绩
    @GetMapping("/Resutls")
    public Object Resutls(Results results) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("username", results.getUsername());
        stringObjectHashMap.put("grade", results.getGrade());
        stringObjectHashMap.put("subjects", results.getSubjects());
        stringObjectHashMap.put("results", results.getResults());
        try {
            int i = UserDao.Resutls(stringObjectHashMap);
            if (i >= 1) {
                return result(true, "添加成功！", null);
            } else {
                return result(false, "添加失败！", null);
            }
        } catch (Exception e) {
            return result(false, "添加失败！" + e, null);
        }
    }

    //    获取科目
    @GetMapping("/getgrade")
    public Object getgrade() {
        try {
            List<Grade> grades = UserDao.showGrade();
            return result(false, "获取成功！", grades);
        } catch (Exception e) {
            return result(false, "获取失败！" + e, null);
        }
    }

    //    查询指定学生成绩是否存在
    @GetMapping("/showresults")
    public Object showresults(@RequestParam(value = "username", required = true) String username, @RequestParam(value = "grade", required = true) String grade, @RequestParam(value = "subjects", required = true) String subjects) {
        try {
            List<Results> showresults = UserDao.showresults(username, grade, subjects);
            if (!showresults.isEmpty()) {
                return result(false, "有数据！", null);
            } else {
                return result(true, "无数据！", null);
            }

        } catch (Exception e) {
            return result(false, "获取失败！" + e, null);
        }
    }

    //  查询所有学生成绩
    @GetMapping("/showallresults")
    public Object showallresults() {
        try {
            List<Results> showallresults = UserDao.showallresults();
            return result(true, "获取成功！", showallresults);

        } catch (Exception e) {
            return result(false, "获取失败！" + e, null);
        }
    }

    //      查询指定班级的所有学生信息
    @GetMapping("/showgraderesults")
    public Object showgraderesults(@RequestParam(value = "grade", required = true) String grade) {
        try {
            List<Results> showgraderesults = UserDao.showgraderesults(grade);
            return result(true, "获取成功！", showgraderesults);

        } catch (Exception e) {
            return result(false, "获取失败！" + e, null);
        }
    }

    //删除指定班级、科目、名称的成绩信息
    @GetMapping("/delectresults")
    public Object delectresults(@RequestParam(value = "username", required = true) String username, @RequestParam(value = "grade", required = true) String grade, @RequestParam(value = "subjects", required = true) String subjects) {
        try {
            int i = UserDao.delectresults(username, grade, subjects);
            if (i >= 1) {
                return result(true, "删除成功！", null);
            } else {
                return result(false, "删除失败！", null);
            }
        } catch (Exception e) {
            return result(false, "删除失败！" + e, null);
        }
    }

    //     查询指定姓名，班级的成绩信息
    @GetMapping("/showusernamegrade")
    public Object showusernamegrade(@RequestParam(value = "username", required = true) String username, @RequestParam(value = "grade", required = true) String grade) {
        try {
            List<Results> showusernamegrade = UserDao.showusernamegrade(username, grade);
            return result(true, "查询成功！", showusernamegrade);

        } catch (Exception e) {
            return result(false, "查询失败！" + e, null);
        }
    }

    //    查询所有学生信息
    @GetMapping("/showstudent")
    public Object showstudent() {
        try {
            List<User> showstudent = UserDao.showstudent();
            return result(true, "查询成功！", showstudent);

        } catch (Exception e) {
            return result(false, "查询失败！" + e, null);
        }
    }
}

